package com.tianqu.tms.core.base;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.ValidatorActionForm;

import com.tianqu.tms.core.util.StringUtil;

/**
 * 
 * @author liugang
 *
 */
public class TmsValidatorActionForm extends ValidatorActionForm{
	
	/**
	 * Serialize Id
	 */
	private static final long serialVersionUID = 1L;
	

	public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) {
		ActionErrors errors =  super.validate(mapping,request);
		if(errors != null && errors.size() > 0){
			UserContext uc = (UserContext)request.getSession().getAttribute(TmsAction.APPLICATION_USER_CONTEXT_KEY);
			request.setAttribute("funcId",getFuncId(mapping,uc));
			if(uc != null) {
				//login的时候
				request.setAttribute(TmsAction.USER_PROFILE_KEY, uc.getUserProfile());
				request.setAttribute(TmsAction.VIEWHELP,uc.getAttribute(TmsAction.VIEWHELP));
			}
		}
		return errors;
	}
	/**
	 * 
	 * @param mapping
	 * @param uc
	 * @return
	 */
	private String getFuncId(ActionMapping mapping,UserContext uc){
		String funcId = ((TmsActionMapping) mapping).getFuncId();
		if(StringUtil.isNullOrBlank(funcId)) {
			funcId = uc.getUserProfile().getFuncId();
		}  else {
			if(uc != null) {
				//login的时候
				uc.getUserProfile().setFuncId(funcId);
			}
		}
		return funcId;
	}
}
